<?php
!defined('M_P') && exit('Forbidden');
!$winduid && Showmsg('not_login');

if ($db_question && ($o_share_qcheck || $o_groups_qcheck)) {
	$qkey = array_rand($db_question);
}
require_once(R_P.'require/showimg.php');
//list($faceurl) = showfacedesign($winddb['icon'],1);
//TODO 我的群组没了?

!$o_groups_open && Showmsg('groups_close');

InitGP(array('a'));

if (empty($a)) {
	InitGP(array('page','u'), null, 2);
	!$u && $u = $winduid;

	$page < 1 && $page = 1;
	if ($u==$winduid) {
		$threaddb = array();
		$db_perpage = 10;
		$total = $db->get_value("SELECT COUNT(*) AS sum FROM pw_cmembers cm LEFT JOIN pw_argument a ON a.gid=cm.colonyid AND a.tpcid='0' WHERE cm.uid=" . pwEscape($u) . ' AND a.tid IS NOT NULL');
		$pages = numofpage($total, $page, ceil($total/$db_perpage), "{$basename}q=$q&");
		$query = $db->query("SELECT cm.colonyid,c.cname,a.tid,a.author,a.authorid,a.lastpost,subject FROM pw_cmembers cm LEFT JOIN pw_colonys c ON cm.colonyid=c.id LEFT JOIN pw_argument a ON cm.colonyid=a.gid AND a.tpcid='0' WHERE cm.uid=" . pwEscape($u) . ' AND a.tid IS NOT NULL ORDER BY a.lastpost DESC' . pwLimit(($page - 1) * $db_perpage, $db_perpage));
		while ($rt = $db->fetch_array($query)) {
			$rt['lastpost'] = get_date($rt['lastpost']);
			$threaddb[] = $rt;
		}
		$username = $windid;
		list($faceimg) = showfacedesign($winddb['icon'],1,'s');

		$group = $apply = array();
		$query = $db->query("SELECT cm.ifadmin,cm.addtime,c.id,c.cname,c.cnimg,c.admin FROM pw_cmembers cm LEFT JOIN pw_colonys c ON cm.colonyid=c.id WHERE cm.uid=" . pwEscape($u) . " ORDER BY cm.colonyid DESC");
		while ($rt = $db->fetch_array($query)) {
			if ($rt['cnimg']) {
				list($rt['cnimg']) = geturl("cn_img/$rt[cnimg]",'lf');
			} else {
				$rt['cnimg'] = $pwModeImg.'/nophoto.gif';
			}
			$rt['addtime'] = get_date($rt['addtime'], 'Y-m-d');
			if ($rt['ifadmin'] == '-1') {
				$apply[] = $rt;
			} else {
				$group[] = $rt;
			}
		}
	} else {
		$friend = $db->get_one("SELECT uid,username,icon FROM pw_members WHERE uid=".pwEscape($u));
		list($faceimg) = showfacedesign($friend['icon'],1,'s');
		$username = $friend['username'];

		$count	= $db->get_one("SELECT COUNT(DISTINCT c.id) AS count FROM pw_cmembers cm LEFT JOIN pw_colonys c ON cm.colonyid=c.id WHERE cm.uid=".pwEscape($u)." AND cm.ifadmin <> '-1'");
		$query	= $db->query("SELECT DISTINCT c.id,c.classid,c.cname,c.cnimg,c.admin,c.members,c.descrip,c.createtime FROM pw_cmembers cm LEFT JOIN pw_colonys c ON cm.colonyid=c.id WHERE cm.uid=".pwEscape($u)." AND cm.ifadmin <> '-1' ORDER BY cm.colonyid DESC");
		while ($rt = $db->fetch_array($query)) {
			if ($rt['cnimg']) {
				list($rt['cnimg']) = geturl("cn_img/$rt[cnimg]",'lf');
			} else {
				$rt['cnimg'] = $pwModeImg.'/nophoto.gif';
			}
			$rt['addtime'] = get_date($rt['addtime'], 'Y-m-d');
			$rt['createtime'] = get_date($rt['createtime'], 'Y-m-d');
			$group[] = $rt;
		}
		$pages = numofpage($count, $page, ceil($total/$db_perpage), "{$basename}q=$q&a=friend&");
	}

	require_once(R_P.'require/header.php');
	require_once PrintEot('m_groups');
	footer();

} elseif ($a == 'all') {

	InitGP(array('page','cid'), null, 2);
	$page < 1 && $page = 1;
	$db_perpage = 10;
	if ($cid) {
		$total = $db->get_value("SELECT cnsum FROM pw_cnclass WHERE cid=" . pwEscape($cid));
		$sql = ' AND classid=' . pwEscape($cid);
	} else {
		$total = $db->get_value("SELECT COUNT(*) FROM pw_colonys");
		$sql = '';
	}
	
	$atc_name = getLangInfo('o','group',false,1);

	$pages = numofpage($total, $page, ceil($total/$db_perpage), "{$basename}q=$q&a=all&");
	$group = array();
	$query = $db->query("SELECT id,classid,cname,members,cnimg,descrip,createtime FROM pw_colonys WHERE 1 $sql ORDER BY id DESC " . pwLimit(($page-1) * $db_perpage, $db_perpage));
	while ($rt = $db->fetch_array($query)) {
		if ($rt['cnimg']) {
			list($rt['cnimg']) = geturl("cn_img/$rt[cnimg]",'lf');
		} else {
			$rt['cnimg'] = $pwModeImg.'/nophoto.gif';
		}
		$rt['createtime'] = get_date($rt['createtime'], 'Y-m-d');
		$rt['link'] = "$db_bbsurl/{$basename}q=group&cyid=$rt[id]";
		$rt['title'] = "$rt[cname] ($rt[link])";

		$group[$rt['id']] = $rt;
	}
	$cMembers = array();
	$colonyids = pwImplode(array_keys($group));
	if ($colonyids) {
		$query = $db->query("SELECT id,ifadmin,colonyid FROM pw_cmembers WHERE colonyid IN ($colonyids) AND uid=".pwEscape($winduid,false));
		while ($rt = $db->fetch_array($query)) {
			$cMembers[$rt['colonyid']] = $rt['ifadmin'];
		}
	}
	$u	= $winduid;
	$username = $windid;
	list($faceimg) = showfacedesign($winddb['icon'],1,'s');

	require_once(R_P.'require/header.php');
	require_once PrintEot('m_groups');
	footer();

} elseif ($a == 'friend') {
	InitGP(array('page','cid'), null, 2);
	$page < 1 && $page = 1;
	$friends	= getFriends($winduid);
	$friends	= array_keys($friends);
	if (count($friends)) {
		$count	= $db->get_one("SELECT COUNT(DISTINCT c.id) AS count FROM pw_cmembers cm LEFT JOIN pw_colonys c ON cm.colonyid=c.id WHERE cm.uid IN(" . pwImplode($friends) . ") AND cm.ifadmin <> '-1'");
		$query	= $db->query("SELECT DISTINCT c.id,c.classid,c.cname,c.cnimg,c.admin,c.members,c.descrip,c.createtime FROM pw_cmembers cm LEFT JOIN pw_colonys c ON cm.colonyid=c.id WHERE cm.uid IN(" . pwImplode($friends) . ") AND cm.ifadmin <> '-1' ORDER BY cm.colonyid DESC");
		while ($rt = $db->fetch_array($query)) {
			if ($rt['cnimg']) {
				list($rt['cnimg']) = geturl("cn_img/$rt[cnimg]",'lf');
			} else {
				$rt['cnimg'] = $pwModeImg.'/nophoto.gif';
			}
			$rt['createtime'] = get_date($rt['createtime'], 'Y-m-d');
			$group[] = $rt;
		}
		$pages = numofpage($count, $page, ceil($total/$db_perpage), "{$basename}q=$q&a=friend&");
	}
	$u	= $winduid;
	$username = $windid;
	list($faceimg) = showfacedesign($winddb['icon'],1,'s');

	require_once(R_P.'require/header.php');
	require_once PrintEot('m_groups');
	footer();
} elseif ($a == 'create') {
	banUser();
	!$o_newcolony && Showmsg('colony_reglimit');

	if ($groupid != 3 && $o_groups && strpos($o_groups,",$groupid,") === false) {
		Showmsg('colony_groupright');
	}

	require_once(R_P.'require/credit.php');
	$moneyname = $credit->cType[$o_moneytype];
	if ($o_createmoney > $credit->get($winduid,$o_moneytype)) {
		Showmsg('colony_creatfailed');
	}

	if ($o_allowcreate && $o_allowcreate <= $db->get_value("SELECT COUNT(*) AS sum FROM pw_colonys WHERE admin=" . pwEscape($windid))) {
		Showmsg('colony_numlimit');
	}
	if (empty($_POST['step'])) {
		$u	= $winduid;
		$username = $windid;
		list($faceimg) = showfacedesign($winddb['icon'],1,'s');
		require_once(R_P.'require/header.php');
		require_once PrintEot('m_groups');
		footer();

	} else {
		PostCheck(1,$o_groups_gdcheck,$o_groups_qcheck);
		InitGP(array('cname','descrip'),'P');
		InitGP(array('cid'), 'P', 2);
		(!$cname || strlen($cname) > 20) && Showmsg('colony_emptyname');
		(!$descrip || strlen($descrip) > 255) && Showmsg('colony_descrip');
		!$cid && Showmsg('colony_class');
		
		if(@include(D_P."data/bbscache/wordsfb.php")){
			if ($wordsfb) {
				foreach ($wordsfb as $key => $value) {
					$banword = (string) stripslashes($key);
					if (strpos($cname,$banword)!==false) {
						Showmsg('title_wordsfb');
					} elseif (strpos($descrip,$banword)!==false) {
						Showmsg('content_wordsfb');
					}
				}
			}
			if ($replace) {
				foreach ($replace as $key => $value) {
					$banword = (string) stripslashes($key);
					if (strpos($cname,$banword)!==false) {
						Showmsg('title_wordsfb');
					} elseif (strpos($descrip,$banword)!==false) {
						Showmsg('content_wordsfb');
					}
				}
			}
		}
		
		$rt = $db->get_one("SELECT id FROM pw_colonys WHERE cname=".pwEscape($newname));
		$rt['id'] > 0 && Showmsg('colony_samename');

		$credit->addLog('hack_cycreate',array($o_moneytype => -$o_createmoney),array(
			'uid'		=> $winduid,
			'username'	=> $windid,
			'ip'		=> $onlineip,
			'cnname'	=> stripslashes($cname)
		));
		$credit->set($winduid,$o_moneytype,-$o_createmoney);

		$db->update("INSERT INTO pw_colonys SET " . pwSqlSingle(array(
				'cname'		=> $cname,
				'classid'	=> $cid,
				'admin'		=> $windid,
				'members'	=> 1,
				'ifcheck'	=> 1,
				'createtime'=> $timestamp,
				'descrip'	=> $descrip
		)));
		$cyid = $db->insert_id();

		$db->update("INSERT INTO pw_cmembers SET " . pwSqlSingle(array(
				'uid'		=> $winduid,
				'username'	=> $windid,
				'ifadmin'	=> 1,
				'colonyid'	=> $cyid
		)));

		$db->update("UPDATE pw_cnclass SET cnsum=cnsum+1 WHERE cid=" . pwEscape($cid));

		pwAddFeed($winduid, 'colony', '', array(
			'lang'		=> 'colony_create',
			'colonyid'	=> $cid,
			'cname'		=> $cname,
			'link'		=> "{$db_bbsurl}/{$basename}q=group&cyid=$cyid"
		));

		refreshto("{$basename}q=group&cyid=$cyid&a=set",'colony_regsuccess');
	}
}
?>